package com.google.code.mochaccino.framework.security;
/**
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Provider;
import org.apache.shiro.authc.Authenticator;
import org.apache.shiro.authz.Authorizer;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.RememberMeManager;
import org.apache.shiro.mgt.SubjectDAO;
import org.apache.shiro.mgt.SubjectFactory;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.session.mgt.SessionManager;

/**
 * Basic Security Provider. This should only be used for advanced/custom use of shiro, otherwise the default settings
 * are appropriate (but you still need to specify the realms).
 */
abstract class SecurityManagerProvider<T extends org.apache.shiro.mgt.SecurityManager> implements Provider<T> {
	private Authenticator     authenticator     = null;
	private Authorizer        authorizer        = null;
	private CacheManager      cacheManager      = null;
	private List<Realm>       realmList         = new ArrayList<Realm>();
	private RememberMeManager rememberMeManager = null;
	private SessionManager    sessionManager    = null;
	private SubjectDAO        subjectDao        = null;
	private SubjectFactory    subjectFactory    = null;

	@Inject
	public SecurityManagerProvider( Realm[] realmList ) {
		Preconditions.checkNotNull( realmList, "No Realm defined." );
		Preconditions.checkState( realmList.length > 0, "0 Realm defined." );
		for ( Realm r : realmList ) {
			this.realmList.add( r );
		}
	}

	protected void configure( DefaultSecurityManager bean ) {
		if ( cacheManager != null ) {
			bean.setCacheManager( cacheManager );
		}

		if ( sessionManager != null ) {
			bean.setSessionManager( sessionManager );
		}

		if ( authenticator != null ) {
			bean.setAuthenticator( authenticator );
		}

		if ( authorizer != null ) {
			bean.setAuthorizer( authorizer );
		}

		if ( rememberMeManager != null ) {
			bean.setRememberMeManager( rememberMeManager );
		}

		if ( subjectDao != null ) {
			bean.setSubjectDAO( subjectDao );
		}

		if ( subjectFactory != null ) {
			bean.setSubjectFactory( subjectFactory );
		}
	}

	protected List<Realm> getRealmList() {
		return realmList;
	}

	public void setAuthenticator( Authenticator authenticator ) {
		this.authenticator = authenticator;
	}

	public void setAuthorizer( Authorizer authorizer ) {
		this.authorizer = authorizer;
	}

	public void setCacheManager( CacheManager cacheManager ) {
		this.cacheManager = cacheManager;
	}

	public void setRememberMeManager( RememberMeManager rememberMeManager ) {
		this.rememberMeManager = rememberMeManager;
	}

	public void setSessionManager( SessionManager sessionManager ) {
		this.sessionManager = sessionManager;
	}

	public void setSubjectDao( SubjectDAO subjectDao ) {
		this.subjectDao = subjectDao;
	}

	public void setSubjectFactory( SubjectFactory subjectFactory ) {
		this.subjectFactory = subjectFactory;
	}
}
